<?php
include("class/pData.class.php");
include("class/pDraw.class.php");
include("class/pImage.class.php");

$myData = new pData();

$ot = "";
$type = isset($_GET['type']) ? $_GET['type'] : 'line';

$tb_name = "tbl_stock_price_for_chart";
$dbh = new PDO('mysql:host=localhost;dbname=test', 'root', '123456');

$id = isset($_GET['id']) ? (int)$_GET['id'] : 1;
$wheres = "where stock_no = " . $id;
$sth = $dbh->prepare("SELECT * FROM $tb_name " . $wheres);
$sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC);

$line2 = array();
$bar = array();
$times = array();
foreach ($results as $keys => $values) :
    $t = $values['datetime'];
    $line2[$t] = $values['current_price'];
    $bar[$t] = $values['volume'];
    $times[] = ($values['datetime'] / 100) . ":" . $values['datetime'] % 100;

   // $myData->addPoints($line2, "Line2");
endforeach;



    $sql = "SELECT  MIN(`current_price`) xiao, MAX(`current_price`) da FROM $tb_name";
    foreach ($dbh->query($sql, PDO::FETCH_ASSOC) as $row) {
        $bottom = (int)$row['xiao'] - 2;
        $top = (int)$row['da'] + 2;
    }

    //$myData->addPoints($l1s, "Line1");
    //$myData->addPoints($line2, "Line2");
  //  $myData->setAxisPosition(0, AXIS_POSITION_RIGHT);
/*
    $myData->addPoints($times, "Times");
    $myData->setSerieDescription("Times","Time");
    $myData->setAbscissa("Times");

*/

$BaseTs = mktime(0, 0, 0, 12, 25, 2011);
$LastIn = 0;
$LastOut = 0;
for ($i = 0; $i <= 1440; $i++) {
    $LastIn = abs($LastIn + rand(-1000, +1000));
    $LastOut = abs($LastOut + rand(-1000, +1000));
    $myData->addPoints($LastIn, "Inbound");
    $myData->addPoints($LastOut, "Outbound");

    $myData->addPoints($BaseTs + $i * 60, "TimeStamp");
}

foreach ($results as $keys => $values) :
    $t = $values['datetime'];
    $line2[$t] = $values['current_price'];
    $bar[$t] = $values['volume'];
    $times[] = ($values['datetime'] / 100) . ":" . $values['datetime'] % 100;

    // $myData->addPoints($line2, "Line2");
endforeach;

$myData->setAxisName(0, "Bandwidth");
//$MyData->setAxisDisplay(0,AXIS_FORMAT_TRAFFIC);
$myData->setSerieDescription("TimeStamp", "time");
$myData->setAbscissa("TimeStamp");
$myData->setXAxisDisplay(AXIS_FORMAT_TIME, "H:00");

    $myPicture = new pImage(480, 300, $myData);

//画格子和标签
    $myPicture->setGraphArea(10, 20, 440, 280);
//设置字体
    $myPicture->setFontProperties(array("FontName" => "fonts/tahoma.ttf", "FontSize" => 10));
    $AxisBoundaries = array(0 => array("Min" => $bottom, "Max" => $top));
    $Settings = array(
        "Mode" => SCALE_MODE_MANUAL,
        "GridR" => 200,
        "GridG" => 200,
        "GridB" => 200,
        "XMargin" => 0,
        "YMargin" => 0,
        //"DrawXLines" => false,
        "GridTicks" => 3,  //格子密度
        "ManualScale" => $AxisBoundaries,
    );
    $myPicture->drawScale($Settings);

//画线
    $line_arr = array(
        "ForceColor" => TRUE,
        "ForceR" => 51,
        "ForceG" => 114,
        "ForceB" => 178);
    $myPicture->drawLineChart($line_arr);

//画区域底线
    $area_arr = array();
    $myPicture->drawAreaChart($area_arr);


    $myPicture->stroke();

?>